package com.liang.response.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.liang.response.constants.ResultHeaderConstants;
import com.liang.response.utils.ClazzUtils;
import lombok.Data;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liangyehao
 * @version 1.0
 * @date 2021/4/28 0:54
 * @content
 */
@Data
public class Result {

    @ExcelProperty(value = "门店",index = 0)
    private String store;

    @ExcelProperty({ResultHeaderConstants.TOTAL,"会话量"})
    private Integer sessionAmount;
    @ExcelProperty({ResultHeaderConstants.TOTAL,"23秒响应量"})
    private Integer responseAmount;
    @ExcelProperty({ResultHeaderConstants.TOTAL,"23秒响应率"})
    private BigDecimal responseRate;
    @ExcelProperty({ResultHeaderConstants.TOTAL,"二次分配客户量"})
    private Integer secondaryDistributionCustomers;
    @ExcelProperty({ResultHeaderConstants.TOTAL,"会话比"})
    private BigDecimal proportionOfSession;

    @ExcelProperty({ResultHeaderConstants.BS,"会话量"})
    private Integer sessionAmountForBs;
    @ExcelProperty({ResultHeaderConstants.BS,"23秒响应量"})
    private Integer responseAmountForBs;
    @ExcelProperty({ResultHeaderConstants.BS,"23秒响应率"})
    private BigDecimal responseRateForBs;
    @ExcelProperty({ResultHeaderConstants.BS,"二次分配客户量"})
    private Integer secondaryDistributionCustomersForBs;
    @ExcelProperty({ResultHeaderConstants.BS,"会话比"})
    private BigDecimal proportionOfSessionForBs;

    @ExcelProperty({ResultHeaderConstants.RENT,"会话量"})
    private Integer sessionAmountForRent;
    @ExcelProperty({ResultHeaderConstants.RENT,"23秒响应量"})
    private Integer responseAmountForRent;
    @ExcelProperty({ResultHeaderConstants.RENT,"23秒响应率"})
    private BigDecimal responseRateForRent;
    @ExcelProperty({ResultHeaderConstants.RENT,"二次分配客户量"})
    private Integer secondaryDistributionCustomersForRent;
    @ExcelProperty({ResultHeaderConstants.RENT,"会话比"})
    private BigDecimal proportionOfSessionForRent;

    @ExcelProperty({ResultHeaderConstants.NEW,"会话量"})
    private Integer sessionAmountForNew;
    @ExcelProperty({ResultHeaderConstants.NEW,"23秒响应量"})
    private Integer responseAmountForNew;
    @ExcelProperty({ResultHeaderConstants.NEW,"23秒响应率"})
    private BigDecimal responseRateForNew;
    @ExcelProperty({ResultHeaderConstants.NEW,"二次分配客户量"})
    private Integer secondaryDistributionCustomersForNew;
    @ExcelProperty({ResultHeaderConstants.NEW,"会话比"})
    private BigDecimal proportionOfSessionForNew;

    @ExcelProperty({ResultHeaderConstants.OTHER,"会话量"})
    private Integer sessionAmountForOther;
    @ExcelProperty({ResultHeaderConstants.OTHER,"23秒响应量"})
    private Integer responseAmountForOther;
    @ExcelProperty({ResultHeaderConstants.OTHER,"23秒响应率"})
    private BigDecimal responseRateForOther;
    @ExcelProperty({ResultHeaderConstants.OTHER,"二次分配客户量"})
    private Integer secondaryDistributionCustomersForOther;
    @ExcelProperty({ResultHeaderConstants.OTHER,"会话比"})
    private BigDecimal proportionOfSessionForOther;

    @ExcelIgnore
    private List<ExcelPropertyDomain> excelPropertyDomains = new ArrayList<>();

    public Result(){
        Field[] declaredFields = ClazzUtils.getAllFields(this);
        for (Field field : declaredFields) {
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            if (annotation==null || annotation instanceof ExcelIgnore) {
                continue;
            }
            field.setAccessible(true);
            String[] value = annotation.value();
            if (value.length==2) {
                excelPropertyDomains.add(new ExcelPropertyDomain(value[0],value[1],field));
            }
        }
    }


    public void build(String kind,CommonIndex commonIndex) throws IllegalAccessException {
        Map<String, List<ExcelPropertyDomain>> collect = excelPropertyDomains.stream()
                .collect(Collectors.groupingBy(ExcelPropertyDomain::getFirstLevel));
        if (!collect.containsKey(kind)) {
            return;
        }

        List<ExcelPropertyDomain> domains = collect.get(kind);
        for (ExcelPropertyDomain domain : domains) {
            for (Map<String, Field> fieldMap : commonIndex.getFieldMaps()) {
                Field indexField = fieldMap.get(domain.getSecondLevel());
                if (indexField!=null) {
                    Field resultField = domain.getField();
                    resultField.set(this,indexField.get(commonIndex));
                }
            }
        }
    }

}
